package cn.itrip.controller;

import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.pojo.ItripHotelRoom;
import cn.itrip.beans.pojo.ItripImage;
import cn.itrip.beans.pojo.ItripLabelDic;
import cn.itrip.beans.vo.ItripImageVO;
import cn.itrip.beans.vo.ItripLabelDicVO;
import cn.itrip.beans.vo.hotelroom.ItripHotelRoomVO;
import cn.itrip.beans.vo.hotelroom.SearchHotelRoomVO;
import cn.itrip.common.DateUtil;
import cn.itrip.common.DtoUtil;
import cn.itrip.common.EmptyUtils;
import cn.itrip.mapper.itripLabelDic.ItripLabelDicMapper;
import cn.itrip.service.itripHotelRoom.ItripHotelRoomService;
import cn.itrip.service.itripImage.ItripImageService;
import cn.itrip.service.itripLabelDic.ItripLabelDicService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import sun.applet.Main;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author: jxm
 * @Description:
 * @Date: 2020/3/18 16:26
 * @Version: 1.0
 */
@Controller
@RequestMapping("/api/hotelroom")
public class HotelRoomController {

    @Resource
    private ItripLabelDicService itripLabelDicService;
    @Resource
    private ItripHotelRoomService itripHotelRoomService;
    @Resource
    private ItripImageService itripImageService;

    /**
     * 查询酒店床型列表
     * @return
     */
    @RequestMapping(value="/queryhotelroombed",method = RequestMethod.GET,produces = "application/json")
    @ResponseBody
    public Dto<ItripLabelDicVO> queryHotelRoombed(){

        try {
            Map<String,Object> map = new HashMap<>();
            map.put("parentId",1);
            List<ItripLabelDic> result =  itripLabelDicService.getItripLabelDicListByMap(map);
            List<ItripLabelDicVO> dicVOList = new ArrayList<>();
            if(EmptyUtils.isNotEmpty(result)){
                ItripLabelDicVO vo = null;
                for(ItripLabelDic itripLabelDic : result){
                    vo = new ItripLabelDicVO();
                    //复制对象
                    BeanUtils.copyProperties(itripLabelDic,vo);
                    dicVOList.add(vo);
                }
            }
            return DtoUtil.returnDataSuccess(dicVOList);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("获取床型失败","100305");
        }

    }
    /**
     * 查询酒店房间列表
     */
    @RequestMapping(value="/queryhotelroombyhotel",method = RequestMethod.POST,produces = "application/json")
    @ResponseBody
    public Dto<List<ItripHotelRoomVO>> queryHotelRoomByHotel(
            @RequestBody SearchHotelRoomVO searchHotelRoomVO){
        if(searchHotelRoomVO.getHotelId() == null || searchHotelRoomVO.getHotelId() == 0){
            return DtoUtil.returnFail("酒店id不能为空","100303");
        }
        if(searchHotelRoomVO.getStartDate() == null || searchHotelRoomVO.getEndDate() == null){
            return DtoUtil.returnFail("酒店入住及退房时间不能为空","100303");
        }
        if(searchHotelRoomVO.getStartDate().getTime() >searchHotelRoomVO.getEndDate().getTime()){
            return DtoUtil.returnFail("入住时间不能大于退房时间","100303");
        }
        Map<String,Object> map = new HashMap<>();
        map.put("hotelId",searchHotelRoomVO.getHotelId());
        map.put("roomBedTypeId",searchHotelRoomVO.getRoomBedTypeId());
        map.put("isHavingBreakfast",searchHotelRoomVO.getIsHavingBreakfast());
        map.put("isBook",searchHotelRoomVO.getIsBook());
        map.put("isCancel",searchHotelRoomVO.getIsCancel());
        map.put("isTimelyResponse",searchHotelRoomVO.getIsTimelyResponse());
        map.put("payType",(searchHotelRoomVO.getPayType()== null ||
                searchHotelRoomVO.getPayType()==3)? null : searchHotelRoomVO.getPayType());
        List<Date> timeList =DateUtil.getBetweenDates(searchHotelRoomVO.getStartDate(),searchHotelRoomVO.getEndDate());
        map.put("timeList",timeList);

        try {
            List<ItripHotelRoom> result = itripHotelRoomService.getItripHotelRoomListByMap(map);
            List<List<ItripHotelRoomVO>> output = new ArrayList<>();
            if(EmptyUtils.isNotEmpty(result)){
                ItripHotelRoomVO vo = null;
                List<ItripHotelRoomVO> tempList = null;
                for(ItripHotelRoom itripHotelRoom : result){
                    vo = new ItripHotelRoomVO();
                    BeanUtils.copyProperties(itripHotelRoom,vo);
                    tempList = new ArrayList<>();
                    tempList.add(vo);
                    output.add(tempList);
                }
            }
            return DtoUtil.returnDataSuccess(output);

        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("系统异常","100303");
        }

    }

    /**
     * 根据酒店房型ID查询酒店房型图片
     * @param targetId
     * @return
     */
    @RequestMapping(value = "/getimg/{targetId}",method = RequestMethod.GET,produces = "application/json")
    @ResponseBody
    public Dto<ItripImageVO> getImg(@PathVariable String targetId){

        if(EmptyUtils.isEmpty(targetId)){
            return DtoUtil.returnFail("酒店房型id不能为空","100302");
        }
        Map<String,Object> map = new HashMap<>();
        map.put("type","1");
        map.put("targetId",Long.valueOf(targetId));
        try {
            List<ItripImage> list = itripImageService.getItripImageListByMap(map);
            List<ItripImageVO> outPut = new ArrayList<>();
            if(EmptyUtils.isNotEmpty(list)){
                ItripImageVO itripImageVO = null;
                for (ItripImage itripImage : list){
                    itripImageVO = new ItripImageVO();
                    BeanUtils.copyProperties(itripImage,itripImageVO);
                    outPut.add(itripImageVO);
                }
            }
            return DtoUtil.returnDataSuccess(outPut);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("获取酒店房型图片失败","100301");
        }

    }


}
